package com.ocom.payconfig.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ocom.common.entity.payconfig.EntityDishesPoint;
import com.ocom.common.response.payconfig.GetPlacePlanListResponse;
import com.ocom.common.vo.payconfig.DishesPointListVo;
import com.ocom.common.vo.payconfig.DishesPointOrgVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface IDaoDishesPoint extends BaseMapper<EntityDishesPoint> {

    /**
     * select 	P.point_id as restPoint , X.point_orgin
     * 			from 	dishes_point as P , xf_point_info as X
     * where 	P.com_id = @comId 	and 	P.point_id in ( @pointOrg ) 	and 	X.point_id = P.point_id
     * 			order by 	X.point_orgin 	desc	limit 1
     * @param comId
     * @param pointOrg
     * @return
     */
    @Select({"<script>",
               "select P.point_id as restPoint , X.point_orgin as pointOrgin \n" +
                       "FROM dishes_point P \n" +
                       "INNER JOIN xf_point_info X ON X.point_id = P.point_id \n"+
                       "where P.com_id=#{comId} "+
                //       "and P.point_id in ( #{pointOrg} ) "+
                       " <when test=\"pointOrg.size()>0 \" > "+
                       "and P.point_id  in" +
                       "<foreach collection='pointOrg' item='item' open='(' separator=',' close=')'>" +
                       "#{item}" +
                       "</foreach>"+
                       " </when> "+
                       "order by X.point_orgin desc  limit 1",
            "</script>"})
    DishesPointOrgVo selectPoint(@Param("comId") Long comId, @Param("pointOrg")List<Integer> pointOrg);


    /**
     * 分库6.1.15菜品摆放表dishes_point查询数据数据DISH：
     * select 	P.dishes_point , X.point_name as pointName , X.point_orgin ,
     * P.dishes_id , D.dishes_name as dishesName , D.photo_url as photoUrl
     * 			from 	dishes_point as P , xf_point_info as X , dishes_info as D
     * where 	P.com_id = @comId 	and 	P.point_id = @restPoint 	and 	P.dishes_id= D.dishes_id
     * 					and 	P.meal_date = @lastDate 	and 	P.meal_no= @mealNo 	and 	P.dishes_point= X.point_id
     * 					and (P.dishes_point = @pointId  	or 	X. point_orgin like “@thisOrg%” )
     * 			order by 	X.parent_point_id , P.dishes_point
     */
    @Select({"<script>",
            "select  P.dishes_point , X.point_name as pointName , X.point_orgin ,  \n" +
                    "P.dishes_id , D.dishes_name as dishesName , D.photo_url as photoUrl  \n" +
                    "from  dishes_point as P , xf_point_info as X , dishes_info as D \n" +
                    "where  P.com_id =#{comId}  and  P.point_id = #{restPoint}  and  P.dishes_id= D.dishes_id \n"+
                    "and  P.meal_date = #{lastDate}  and  P.meal_no= #{mealNo}  and  P.dishes_point= X.point_id "+
                    "and   X. point_orgin is not null   "+
                    "and (P.dishes_point = #{pointId} "+
                    " <when test=\"thisOrg.size()>0 \" > "+
                    "or  " +
                    "<foreach collection='thisOrg' item='item' open='( ' separator='or' close=')'>" +
                    "  X. point_orgin like   concat('%,', #{item}, ',%')    " +
                    "</foreach>"+
                    " </when> "+
                    ")" +
                    "order by  X.parent_point_id , P.dishes_point",
            "</script>"})
    List<DishesPointListVo>  selectPointDishes(@Param("comId") Long comId,
                                               @Param("restPoint")Integer restPoint,
                                               @Param("lastDate") String lastDate,
                                               @Param("mealNo")Integer mealNo,
                                               @Param("pointId")Integer pointId,
                                               @Param("thisOrg") List<Integer> thisOrg

    );


    @Select({"<script>",
            "select P.meal_date as mealDate , P.meal_no as mealNo , M.meal_name as mealName\n" +
                    "   from  dishes_point as P , xf_meal_info as M\n" +
                    "   where P.com_id = #{comId}  and P.point_id = #{pointId}  and M.meal_no = P.meal_no" +
               //     "     and  M.start_date <= T+15  and  M.stop_date >= T+0\n" +
                    "  and       P.meal_date <![CDATA[ >= ]]> #{start}  \n" +
                    "   group by  P.meal_date,P.meal_no  \n" +
                    "   order by  P.meal_date,P.meal_no ",
            "</script>"})
    List<GetPlacePlanListResponse>  getPlacePlanList(@Param("comId") Long comId,
                                                     @Param("merchId")Integer merchId,
                                                     @Param("pointId")Integer pointId,
                                                     @Param("start")String start);


    @Select({"<script>",
            "select   M.dishes_id as dishesId , M.price_mode as priceMode , M.dishes_point as placeId , \n" +
                    "D.dishes_name as dishesName , P.point_name as placeName \n" +
                    "      from   dishes_point as M \n" +
                    "      left join  dishes_info as D   on  D.dishes_id = M.dishes_id \n" +
                    "left join  xf_point_info as P   on  P.point_id = M.dishes_point  \n" +
                    "where   M.com_id = #{comId}   and   M.point_id = #{pointId}   and   M.meal_date= #{mealDate}\n" +
                    "          and   M. meal_no= #{mealNo} \n" +
                    "      order by   M.dishes_id",
            "</script>"})
    public List<EntityDishesPoint> getPointPlace(@Param("comId") Long comId,
                                                 @Param("pointId") Integer pointId,
                                                 @Param("mealDate") String mealDate,
                                                 @Param("mealNo") Integer mealNo);
}
